package cn.com.elite;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import cn.com.elite.util.Util;

public class GetInfo extends HttpServlet{
	private static final Log log=LogFactory.getLog(GetInfo.class);
	public EliteForBean getbean(){
		ServletContext application;   
        WebApplicationContext wac;   
        application = getServletContext();   
        wac=WebApplicationContextUtils.getWebApplicationContext(application);//获取spring的context  
        cn.com.elite.EliteForBean ebean=(cn.com.elite.EliteForBean)wac.getBean("ebean");  
        return ebean;
	}
	public void doGet(HttpServletRequest request, HttpServletResponse response){
		log.info("请用POST方式");
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException{
//		log.info("2222:"+request.getCharacterEncoding());
		request.setCharacterEncoding("UTF-8");
		String URL=Util.filter(request.getParameter("URL"));
		String Method=Util.filter(request.getParameter("Method"));
		String FromCode=Util.filter(request.getParameter("FromCode"));
		String ToCode=Util.filter(request.getParameter("ToCode"));
		String Content=Util.filter(request.getParameter("Content"));
		String HeadStr=Util.filter(request.getParameter("HeadStr"));
		String Guid=Util.filter(request.getParameter("Guid"));
		log.info("URL:"+URL);
		log.info("Method:"+Method);
		log.info("FromCode:"+FromCode);
		log.info("ToCode:"+ToCode);
		log.info("Content:"+Content);
		log.info("HeadStr:"+HeadStr);
		log.info("Guid:"+Guid);
		Map map=new HashMap();
		map.put("URL",URL);
		map.put("Method",Method);
		map.put("FromCode",FromCode);
		map.put("ToCode",ToCode);
		map.put("Content",Content);
		map.put("HeadStr",HeadStr);
		map.put("Guid",Guid);
		map.put("count",0);
		String CONTENT=URL+"--"+Method+"--"+FromCode+"--"+FromCode+"--"+Content+"--"+HeadStr+"--"+Guid;
		CONTENT=CONTENT.replace("'","\"");
		
		String sql=" update eliteforward set CONTENT1='"+CONTENT+"' where GUID='"+Guid+"'";
		cn.com.elite.EliteForBean ebean=getbean();
		int result=ebean.update_sql(sql);
		log.info(sql+":"+result);
		
		try{
			if(EliteStartThread.thread_list!=null&&EliteStartThread.thread_list.size()>0){
				int a=0;
				for(int i=0;i<EliteStartThread.thread_list.size();i++){
					Map map1=(Map)EliteStartThread.thread_list.get(i);
					if(map1!=null&&map1.size()>0){
						Map map2=(Map)map1.get(Guid);
						if(map2!=null){
							synchronized(map1){
								map1.remove(Guid);
								map1.put(Guid,map);
							}
							a=1;
							break;
						}
					}
					
				}
				if(a==0){
					int maplength=((Map)EliteStartThread.thread_list.get(0)).size();
					int length_judge=0;
					for(int i=0;i<EliteStartThread.thread_list.size();i++){
						Map map1=(Map)EliteStartThread.thread_list.get(i);
						if(map1.size()<maplength){
							maplength=map1.size();
							length_judge=i;
						}
					}
					
//					String sql=" insert into eliteforward(GUID,CONTENT1)values('"+Guid+"','"+CONTENT+"')";
//					cn.com.elite.EliteForBean ebean=getbean();
//					int result=ebean.update_sql(sql);
//					log.info(sql+":"+result);
					synchronized(((Map)EliteStartThread.thread_list.get(length_judge))){
						((Map)EliteStartThread.thread_list.get(length_judge)).put(Guid,map);
					}
				}
			}
		}catch(Exception e){
			log.error(Util.getERRO(e));
		}	
		
		
		
//		try{
//			synchronized(EliteStartThread.list){
//				if(EliteStartThread.list!=null&&EliteStartThread.list.size()>0){
//					Map map1=(Map)EliteStartThread.list.get(Guid);
//					if(map1==null){
//						EliteStartThread.list.put(Guid,map);
//						String sql=" insert into eliteforward(GUID,CONTENT)values('"+Guid+"','"+CONTENT+"')";
//						cn.com.elite.EliteForBean ebean=getbean();
//						int result=ebean.update_sql(sql);
//						log.info(sql+":"+result);
//					}else{
//						EliteStartThread.list.remove(Guid);
//						EliteStartThread.list.put(Guid,map);
//					}
//				}else{
//					EliteStartThread.list.put(Guid,map);
//					String sql=" insert into eliteforward(GUID,CONTENT)values('"+Guid+"','"+CONTENT+"')";
//					cn.com.elite.EliteForBean ebean=getbean();
//					int result=ebean.update_sql(sql);
//					log.info(sql+":"+result);
//				}
//			}
//			
//		}catch(Exception e){
//			log.error(Util.getERRO(e));
//		}	
		
		try {
			request.setCharacterEncoding("UTF-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			log.error(Util.getERRO(e));
		}
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out;
		try {
			out=response.getWriter();
			out.print("1");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			log.error(Util.getERRO(e));
		}
	}
}
